package com.winit.core.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * Created by wangjian on 16/8/9.
 */
public abstract class Task implements Callable<Task.TaskStatus> {

    //task唯一标识
    public String taskId;
    //任务返回状态
    public TaskStatus taskStatus;
    //是否停止任务
    public boolean isTaskStop = Boolean.FALSE;
    //运行时间 单位:秒
    public Integer expireTime = 1800;
    //信息
    public String message;

    public BaseTaskContainer taskContainer;
    //是否为暂停后继续执行
    public boolean isContinue = false;

    /**
     * 任务开始时间
     * 默认在创建task时初始化
     * 当ChainBaseTask.taskNodeList中多个任务按顺序执行时,需在taskNode的call方法中初始化
     */
    public long startTime = 0L;

    private static final Logger logger = LoggerFactory.getLogger(Task.class);

    public Task(String taskId, Integer expireTime) {
        this.taskId = taskId;
        if (expireTime != 0) {
            this.expireTime = expireTime;
        }
        this.startTime = new Date().getTime();
    }

    public String getTaskId() {
        return this.taskId;
    }

    public void setTaskStatus(TaskStatus taskStatus) {
        this.taskStatus = taskStatus;
    }

    public void saveContainer(BaseTaskContainer taskContainer) {
        this.taskContainer = taskContainer;
    }

    //任务执行完成后,返回指定状态
    public abstract TaskStatus call() throws Exception;

    //获取线程已运行时间  秒
    public Integer getRunTime() {
        Date now = new Date();
        long run = now.getTime() - this.startTime;
        return Math.round(new Long(run).intValue() / 1000);
    }

    //获取线程剩余时间  秒
    public Integer getLeftTime() {
        if (this.expireTime == 0) {
            return 0;
        }
        Integer run = this.getRunTime();
        return this.expireTime - run;
    }

    //外部调用  停止当前线程
    public void stopTask() {
        logger.info("Task:停止任务,taskId=" + this.taskId);
        this.isTaskStop = Boolean.TRUE;
    }

    public enum TaskStatus {
        //发布状态
        STATUS_SUCCESS("success"),//成功
        STATUS_ERROR("error"),//失败
        STATUS_RUNNING("running"),//运行中
        STATUS_TIMEOUT("timeout"),//超时
        STATUS_STOP("stop"),//任务被停止
        STATUS_PAUSE("pause"),//暂停
        STATUS_NOT_START("non"),//未开始
        STATUS_CONTINUE("continue"),//任务暂停后继续
        STATUS_PREPARE("prepare"),//已准备
        STATUS_EXCEPTION("exception"),//异常
        STATUS_PARAM_ERROR("param_error"),//参数错误
        STATUS_WARNING("warning");//警告

        private String value;

        TaskStatus(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        private static final Map<String, TaskStatus> stringToEnum = new HashMap<>();

        static {
            for (TaskStatus status : values()) {
                stringToEnum.put(status.getValue(), status);
            }
        }

        public static TaskStatus fromString(String value) {
            return stringToEnum.get(value);
        }

    }
}
